// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   questionesRegister_ok.java

package hr.engage.test;

import include.nseer_cookie.NseerId;
import include.nseer_cookie.counter;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class questionesRegister_ok extends HttpServlet
{

	ServletContext application;
	HttpSession session;

	public questionesRegister_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			HttpSession session = request.getSession();
			java.io.PrintWriter out = response.getWriter();
			nseer_db_backup1 hr_db = new nseer_db_backup1(dbApplication);
			if (hr_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String time = "";
				Date now = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
				time = formatter.format(now);
				counter count = new counter(dbApplication);
				ValidataNumber validata = new ValidataNumber();
				int amount_sum = 0;
				String human_major_first_kind_ID = "";
				String human_major_first_kind_name = "";
				String human_major_second_kind_ID = "";
				String human_major_second_kind_name = "";
				String human_major_first_kind = request.getParameter("select4");
				if (human_major_first_kind != null && !human_major_first_kind.equals(""))
				{
					for (StringTokenizer tokenTO4 = new StringTokenizer(human_major_first_kind, "/"); tokenTO4.hasMoreTokens();)
					{
						human_major_first_kind_ID = tokenTO4.nextToken();
						human_major_first_kind_name = tokenTO4.nextToken();
					}

				}
				String human_major_second_kind = request.getParameter("select5");
				if (human_major_second_kind != null && !human_major_second_kind.equals(""))
				{
					for (StringTokenizer tokenTO5 = new StringTokenizer(human_major_second_kind, "/"); tokenTO5.hasMoreTokens();)
					{
						human_major_second_kind_ID = tokenTO5.nextToken();
						human_major_second_kind_name = tokenTO5.nextToken();
					}

				}
				String register = request.getParameter("register");
				String register_time = request.getParameter("register_time");
				String test_limited_time = request.getParameter("test_limited_time");
				String kind_amount = request.getParameter("kind_amount");
				int amount1 = Integer.parseInt(kind_amount);
				int p = 0;
				int pp = 0;
				for (int i = 1; i <= amount1; i++)
				{
					String temkind = (new StringBuilder("kind")).append(i).toString();
					String temgroup_amount = (new StringBuilder("group_amount")).append(i).toString();
					String first_kind = request.getParameter(temkind);
					String group_amount = request.getParameter(temgroup_amount);
					int amount2 = Integer.parseInt(group_amount);
					String id_group = "";
					for (int j = 1; j <= amount2; j++)
					{
						String temamount = (new StringBuilder("amount")).append(i).append(j).toString();
						String amount = request.getParameter(temamount);
						int amount3 = 0;
						if (amount != null && !amount.equals(""))
							if (!validata.validata(amount))
								p++;
							else
								pp = (int)((double)pp + Double.parseDouble(amount));
						if (!test_limited_time.equals("") && (!validata.validata(test_limited_time) || Double.parseDouble(test_limited_time) == 0.0D))
							p++;
					}

				}

				if (p == 0 && !test_limited_time.equals("") && pp != 0)
				{
					int n = 0;
					for (int i = 1; i <= amount1; i++)
					{
						String temkind = (new StringBuilder("kind")).append(i).toString();
						String temgroup_amount = (new StringBuilder("group_amount")).append(i).toString();
						String first_kind = request.getParameter(temkind);
						String group_amount = request.getParameter(temgroup_amount);
						int amount2 = Integer.parseInt(group_amount);
						String id_group = "";
						for (int j = 1; j <= amount2; j++)
						{
							String temgroup = (new StringBuilder("group")).append(i).append(j).toString();
							String temamount = (new StringBuilder("amount")).append(i).append(j).toString();
							String temamounta = (new StringBuilder("amounta")).append(i).append(j).toString();
							String group = request.getParameter(temgroup);
							String amount = request.getParameter(temamount);
							String amounta = request.getParameter(temamounta);
							int amount3 = 0;
							if (amount.equals("") || amount == null)
								amount3 = 0;
							else
								amount3 = Integer.parseInt(amount);
							if (amount3 > Integer.parseInt(amounta))
								n++;
						}

					}

					if (n == 0)
					{
						String test_ID = NseerId.getId(getClass().getResource("").toString(), (String)dbSession.getAttribute("unit_db_name"));
						for (int i = 1; i <= amount1; i++)
						{
							String temkind = (new StringBuilder("kind")).append(i).toString();
							String temgroup_amount = (new StringBuilder("group_amount")).append(i).toString();
							String first_kind = request.getParameter(temkind);
							String group_amount = request.getParameter(temgroup_amount);
							int amount2 = Integer.parseInt(group_amount);
							String id_group = "";
							for (int j = 1; j <= amount2; j++)
							{
								String temgroup = (new StringBuilder("group")).append(i).append(j).toString();
								String temamount = (new StringBuilder("amount")).append(i).append(j).toString();
								String group = request.getParameter(temgroup);
								String amount = request.getParameter(temamount);
								int amount3 = 0;
								if (amount.equals("") || amount == null)
									amount3 = 0;
								else
									amount3 = Integer.parseInt(amount);
								amount_sum += amount3;
								String sql = (new StringBuilder("select * from hr_questiones where first_kind_name='")).append(first_kind).append("' and second_kind_name='").append(group).append("' order by rand() limit ").append(amount3).toString();
								for (ResultSet rs = hr_db.executeQuery(sql); rs.next();)
									id_group = (new StringBuilder(String.valueOf(id_group))).append(rs.getString("id")).append(",").toString();

							}

							if (!id_group.equals(""))
							{
								String sql1 = (new StringBuilder("insert into hr_test_details(test_ID,question_first_kind,question_id_group) values ('")).append(test_ID).append("','").append(first_kind).append("','").append(id_group).append("')").toString();
								hr_db.executeUpdate(sql1);
							}
						}

						int totalsum = amount_sum * 2;
						String sql2 = (new StringBuilder("insert into hr_test(test_ID,register,register_time,human_major_first_kind_ID,human_major_first_kind_name,human_major_second_kind_ID,human_major_second_kind_name,questiones_amount,max_total_points,test_limited_time) values ('")).append(test_ID).append("','").append(register).append("','").append(register_time).append("','").append(human_major_first_kind_ID).append("','").append(human_major_first_kind_name).append("','").append(human_major_second_kind_ID).append("','").append(human_major_second_kind_name).append("','").append(amount_sum).append("','").append(totalsum).append("','").append(test_limited_time).append("')").toString();
						hr_db.executeUpdate(sql2);
						String sql3 = (new StringBuilder("select * from hr_config_major_second_kind where first_kind_ID='")).append(human_major_first_kind_ID).append("' and second_kind_ID='").append(human_major_second_kind_ID).append("'").toString();
						ResultSet rs3 = hr_db.executeQuery(sql3);
						if (rs3.next())
						{
							int test_amount = rs3.getInt("test_amount") + 1;
							String sql4 = (new StringBuilder("update hr_config_major_second_kind set test_amount='")).append(test_amount).append("' where first_kind_ID='").append(human_major_first_kind_ID).append("' and second_kind_ID='").append(human_major_second_kind_ID).append("'").toString();
							hr_db.executeUpdate(sql4);
						}
						response.sendRedirect("hr/engage/test/questionesRegister_ok_a.jsp");
					} else
					{
						response.sendRedirect("hr/engage/test/questionesRegister_ok_b.jsp");
					}
				} else
				{
					response.sendRedirect("hr/engage/test/questionesRegister_ok_c.jsp");
				}
				hr_db.commit();
				hr_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception exception) { }
	}
}
